﻿using System;
using System.Collections.Generic;
using System.Linq;
using Roslyn.Compilers.CSharp;

namespace BITS.PreCompiler.CSharp.Syntax
{
    public partial class Enum : BaseType<Enum, EnumDeclarationSyntax>, IHaveMember<EnumMemberDeclarationSyntax>
        , IEnumerable<Enum.Field>
        , IEnumerable<IEnumerable<Enum.Field>>
    {
        public partial class Field : Member<Field, EnumMemberDeclarationSyntax>
        {
            # region • Ctor's •

            /// <summary>
            /// Initializes a new instance of the <see cref="Field"/> class.
            /// </summary>
            /// <param name="syntax">The method declaration syntax.</param>
            /// <param name="parent">The parent.</param>
            public Field(EnumMemberDeclarationSyntax syntax, IBaseSyntaxNode parent = null)
                : base(syntax, parent)
            {
                if (syntax == null)
                    return;

                this.ExtractAttributes(syntax.AttributeLists);

                this.Expression = syntax.EqualsValue.ToFullString();
            }

            # endregion Ctor's

            # region • Methods •

            /// <summary>
            /// Builds the class decleration.
            /// </summary>
            /// <returns></returns>
            private EnumMemberDeclarationSyntax BuildMemberDecleration()
            {
                return Roslyn.Compilers.CSharp.Syntax.EnumMemberDeclaration(
                    attributeLists: this.AttributeDeclarationList,
                    identifier:     this.Identifier,
                    equalsValue:    this.EqualsValueClause);
            }

            # endregion Methods
        }

        # region • Ctor's •

        # endregion Ctor's

        # region • Methods •

        private void ExtractImplementors(IEnumerable<TypeSyntax> implementorList)
        {
            foreach (var implementor in implementorList)
                this.implementors.Add(new Type(implementor));
        }

        /// <summary>
        /// Builds the class decleration.
        /// </summary>
        /// <returns></returns>
        private EnumDeclarationSyntax BuildEnumDecleration()
        {
            return Roslyn.Compilers.CSharp.Syntax.EnumDeclaration(
                attributeLists: this.AttributeDeclarationList,
                modifiers:      this.ModifierList,
                identifier:     this.Identifier,
                baseList:       this.BaseList,
                members:        this.MemberList);
        }

        /// <summary>
        /// Adds the specified append line.
        /// </summary>
        /// <param name="code">The code.</param>
        public override BaseSyntaxNode<Enum, EnumDeclarationSyntax> Add(String code)
        {
            if (String.IsNullOrEmpty(code))
                return this;

            this.ExtractMembers(this.ParseEnumMembers(code)
                .ToList());

            base.Add(code);

            return this;
        }

        # endregion Methods
    }
}
